home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / incoming / libgr-2.000 / libgr-2 / libgr-2.0.3 / tiff / tif_unix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-20  |  4.6 KB  |  210 lines

  1. /* /usr/local/src/CVS/libgr/tiff/tif_unix.c,v 1.2 1995/08/20 12:51:31 neal Exp */
  2.  
  3. /*
  4.  * Copyright (c) 1988-1995 Sam Leffler
  5.  * Copyright (c) 1991-1995 Silicon Graphics, Inc.
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26.  
  27. /*
  28.  * TIFF Library UNIX-specific Routines.
  29.  */
  30. #include "tiffiop.h"
  31. #include <sys/types.h>
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34.  
  35. static tsize_t
  36. _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
  37. {
  38.     return ((tsize_t) read((int) fd, buf, (size_t) size));
  39. }
  40.  
  41. static tsize_t
  42. _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
  43. {
  44.     return ((tsize_t) write((int) fd, buf, (size_t) size));
  45. }
  46.  
  47. static toff_t
  48. _tiffSeekProc(thandle_t fd, toff_t off, int whence)
  49. {
  50.     return ((toff_t) lseek((int) fd, (off_t) off, whence));
  51. }
  52.  
  53. static int
  54. _tiffCloseProc(thandle_t fd)
  55. {
  56.     return (close((int) fd));
  57. }
  58.  
  59. #include <sys/stat.h>
  60.  
  61. static toff_t
  62. _tiffSizeProc(thandle_t fd)
  63. {
  64. #ifdef _AM29K
  65.     long fsize;
  66.     return ((fsize = lseek((int) fd, 0, SEEK_END)) < 0 ? 0 : fsize);
  67. #else
  68.     struct stat sb;
  69.     return (toff_t) (fstat((int) fd, &sb) < 0 ? 0 : sb.st_size);
  70. #endif
  71. }
  72.  
  73. #ifdef HAVE_MMAP
  74. #include <sys/mman.h>
  75.  
  76. static int
  77. _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
  78. {
  79.     toff_t size = _tiffSizeProc(fd);
  80.     if (size != (toff_t) -1) {
  81.         *pbase = (tdata_t)
  82.             mmap(0, size, PROT_READ, MAP_SHARED, (int) fd, 0);
  83.         if (*pbase != (tdata_t) -1) {
  84.             *psize = size;
  85.             return (1);
  86.         }
  87.     }
  88.     return (0);
  89. }
  90.  
  91. static void
  92. _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
  93. {
  94.     (void) fd;
  95.     (void) munmap(base, (off_t) size);
  96. }
  97. #else /* !HAVE_MMAP */
  98. static int
  99. _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
  100. {
  101.     (void) fd; (void) pbase; (void) psize;
  102.     return (0);
  103. }
  104.  
  105. static void
  106. _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
  107. {
  108.     (void) fd; (void) base; (void) size;
  109. }
  110. #endif /* !HAVE_MMAP */
  111.  
  112. /*
  113.  * Open a TIFF file descriptor for read/writing.
  114.  */
  115. TIFF*
  116. TIFFFdOpen(int fd, const char* name, const char* mode)
  117. {
  118.     TIFF* tif;
  119.  
  120.     tif = TIFFClientOpen(name, mode,
  121.         (thandle_t) fd,
  122.         _tiffReadProc, _tiffWriteProc,
  123.         _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
  124.         _tiffMapProc, _tiffUnmapProc);
  125.     if (tif)
  126.         tif->tif_fd = fd;
  127.     return (tif);
  128. }
  129.  
  130. /*
  131.  * Open a TIFF file for read/writing.
  132.  */
  133. TIFF*
  134. TIFFOpen(const char* name, const char* mode)
  135. {
  136.     static const char module[] = "TIFFOpen";
  137.     int m, fd;
  138.  
  139.     m = _TIFFgetMode(mode, module);
  140.     if (m == -1)
  141.         return ((TIFF*)0);
  142. #ifdef _AM29K
  143.     fd = open(name, m);
  144. #else
  145.     fd = open(name, m, 0666);
  146. #endif
  147.     if (fd < 0) {
  148.         TIFFError(module, "%s: Cannot open", name);
  149.         return ((TIFF *)0);
  150.     }
  151.     return (TIFFFdOpen(fd, name, mode));
  152. }
  153.  
  154. void*
  155. _TIFFmalloc(tsize_t s)
  156. {
  157.     return (malloc((size_t) s));
  158. }
  159.  
  160. void
  161. _TIFFfree(tdata_t p)
  162. {
  163.     free(p);
  164. }
  165.  
  166. void*
  167. _TIFFrealloc(tdata_t p, tsize_t s)
  168. {
  169.     return (realloc(p, (size_t) s));
  170. }
  171.  
  172. void
  173. _TIFFmemset(tdata_t p, int v, tsize_t c)
  174. {
  175.     memset(p, v, (size_t) c);
  176. }
  177.  
  178. void
  179. _TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
  180. {
  181.     memcpy(d, s, (size_t) c);
  182. }
  183.  
  184. int
  185. _TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
  186. {
  187.     return (memcmp(p1, p2, (size_t) c));
  188. }
  189.  
  190. static void
  191. unixWarningHandler(const char* module, const char* fmt, va_list ap)
  192. {
  193.     if (module != NULL)
  194.         fprintf(stderr, "%s: ", module);
  195.     fprintf(stderr, "Warning, ");
  196.     vfprintf(stderr, fmt, ap);
  197.     fprintf(stderr, ".\n");
  198. }
  199. TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
  200.  
  201. static void
  202. unixErrorHandler(const char* module, const char* fmt, va_list ap)
  203. {
  204.     if (module != NULL)
  205.         fprintf(stderr, "%s: ", module);
  206.     vfprintf(stderr, fmt, ap);
  207.     fprintf(stderr, ".\n");
  208. }
  209. TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
  210.